package com.itheima.health.service.impl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.*;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetMealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
 * @author zhangmeng
 * @description 套餐SEVICE实现类
 * @date 2019/9/26
 **/
@Service
@Slf4j
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealDao setMealDao;
    @Resource
    private CheckGroupDao checkGroupDao;
    @Resource
    private CheckGroupCheckitemDao checkGroupCheckitemDao;
    @Resource
    private CheckItemDao checkItemDao;
    @Resource
    private SetmealCheckgroupDao setmealCheckgroupDao;
    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        log.info("[套餐-添加]data:{},checkgroupIds:{}",setmeal,checkgroupIds);
        // 调用DAO数据入库
        // 插入基本数据
        setMealDao.insert(setmeal);
        // 插入关联数据
        for (Integer checkgroupId : checkgroupIds) {
            setMealDao.insertSetMealAndCheckGroup(setmeal.getId(),checkgroupId);
        }
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //设置分页参数
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //DAO查询数据
        Page<Setmeal> page = setMealDao.selectByCondition(queryPageBean.getQueryString());
        //封装返回值
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public long countByImg(String img) {
        return setMealDao.countByImg(img);
    }

    @Override
    public List<Setmeal> findAll() {
        //调用DAO查询所有
        return setMealDao.selectAll();
    }

    @Override
    public Setmeal findById(Integer id) {
        //调用DAO查询数据
        return setMealDao.selectById(id);
    }

    /**
     * C端查询全部套餐
     *
     * @return 并携带查询到的数据集合
     */
    @Override
    public List<Setmeal> selectSetmealAll() {

        return setMealDao.selectSetmealAll();
    }

    /**
     * C端根据ID查询套餐数据
     *
     * @param id 套餐id
     * @return 查询到的数据
     */
    @Override
    public Setmeal selectSetmealById(Integer id) {
        // 根据id查询套餐信息
        Setmeal setmeal = setMealDao.selectById(id);
        // 根据套餐id 查询所对应的检查组数据id
        List<Integer> checkGroupList = setmealCheckgroupDao.selectSetmealById(id);
        // 创建一个集合保存检查组
        List<CheckGroup> checkGroups = new ArrayList<>();
        // 遍历第一个集合，用检查组id查询检查数据并赋值给第二个集合
        checkGroupList.forEach(new Consumer<Integer>() {
            @Override
            public void accept(Integer integer) {
                CheckGroup checkGroup = checkGroupDao.selectById(integer);
                checkGroups.add(checkGroup);
            }
        });

        checkGroups.forEach(new Consumer<CheckGroup>() {
            @Override
            public void accept(CheckGroup checkGroup) {
                // 创建一个检查项集合用来给检查组的检查项集合赋值
                List<CheckItem> checkItemList = new ArrayList<>();
                List<Integer> checkitemIdList = checkGroupCheckitemDao.selectCheckitemListId(checkGroup.getId());
                checkitemIdList.forEach(new Consumer<Integer>() {
                    @Override
                    public void accept(Integer integer) {
                        CheckItem checkItem = checkItemDao.selectById(integer);
                        checkItemList.add(checkItem);
                    }
                });
                checkGroup.setCheckItems(checkItemList);
            }
        });
        setmeal.setCheckGroups(checkGroups);
        return setmeal;
    }
}
